<template>
  <a-card title="网站配置">
    <a-form :form="form" :label-col="{ span: 2 }" :wrapper-col="{ span: 10 }">
      <a-form-item v-show="false">
        <a-input v-decorator="['md5']"/>
      </a-form-item>
      <a-form-item label="网站名称">
        <a-input v-decorator="['webName',{rules: [{required: true, message:'请输入名称'}]}]"/>
      </a-form-item>
      <a-form-item label="logo图片">
        <a-input v-decorator="['logoPic',{rules: [{required: true, message:'请输入名称'}]}]"/>
      </a-form-item>
      <a-form-item label="网站域名">
        <a-input v-decorator="['host',{rules: [{required: true, message:'请输入网站域名，用于第三方登录回调'}]}]" placeholder="请输入网站域名，用于第三方登录回调"/>
      </a-form-item>
      <a-form-item :wrapperCol="{offset:2}">
        <a-button type="primary" @click="handlerOk">保存</a-button>
      </a-form-item>
    </a-form>
  </a-card>
</template>

<script>
import { saveConfig, queryConfig } from '@/api/manage'

export default {
  name: 'WebConfig',
  data () {
    return {
      form: this.$form.createForm(this),
      record: {}
    }
  },
  components: {
  },
  methods: {
    handlerOk () {
      this.form.validateFields((errors, values) => {
        if (!errors) {
          var data = {
            configType: 'WEB_CONFIG',
            configKey: 'WEB',
            configName: '网站配置',
            configContent: JSON.stringify(values),
            configMd5: this.record.configMd5
          }
          saveConfig(data).then(res => {
            this.$message.info('保存成功')
            this.$refs.table.refresh(true)
            this.visible = false
          })
        }
      })
    }
  },
  mounted () {
    queryConfig({
      configTypeEnum: 'WEB_CONFIG',
      configKeyEnum: 'WEB',
      queryContent: true
    }).then(res => {
      if (res.data && res.data.length === 1) {
        this.$nextTick(() => {
          this.form.setFieldsValue(JSON.parse(res.data[0].configContent))
          this.record = res.data[0]
        })
      }
    })
  }
}
</script>
